package org.metasketch.demo.diagrams.PSD;

import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.StrokeType;
import javafx.scene.transform.Rotate;

import org.metasketch.demo.model.CoordinationStep;
import org.metasketch.diagram.javafx.graphics.BaseGraphic;
import org.metasketch.diagram.javafx.graphics.CustomLabel;
import org.metasketch.diagram.javafx.graphics.Resizableness;
import org.metasketch.graphics.GraphicNode;

public class CoordinationStepNode extends BaseGraphic {

	CustomLabel nameLabel = new CustomLabel("Untitled");
	CustomLabel identifierLabel = new CustomLabel("Untitled");

	Rectangle rect;// = new Rectangle();

	Color lineColor = Color.BLACK;
	Color backColor = Color.web("f5f5f5");
	// Color darkBackColor = Color.web("808080");
	private Circle circle;

	int side = 20;
	private Rotate rz;

	public CoordinationStepNode() {
		super(new GraphicNode(0, 0, 40, 40));

		setResizable(Resizableness.HORIZONTAL);
	}

	@Override
	protected void continueSetup() {

		setupGraphic(graphicNode.getX(), graphicNode.getY(),
				graphicNode.getWidth(), graphicNode.getHeight());

	}

	private void setupGraphic(int x, int y, int width, int height) {

		this.width = width;
		this.height = height;

		setX(x);
		setY(y);

		rect = new Rectangle(1, 1, side * 2, side * 2);
		rect.setStroke(lineColor);
		rect.setStrokeWidth(2);
		rect.setFill(backColor);

		circle = new Circle();
		circle.setStroke(lineColor);
		circle.setStrokeWidth(2);
		circle.setFill(backColor);

		circle.setCenterX(side + 1);
		circle.setCenterY(side + 1);
		circle.setRadius(side);

		nameLabel.setGraphic(this);
		nameLabel.setVerticallyCentered(false);
		nameLabel.setHorizontallyCentered(true);
		nameLabel.setFieldNameEditing("parentTransaction.identifier", true);

		identifierLabel.setGraphic(this);
		identifierLabel.setVerticallyCentered(true);
		identifierLabel.setFieldNameEditing("kind", false);
		identifierLabel
				.setReadScript("String text = semantic.kind.toString(); return text.substring(0,3);");

		// rz = new Rotate();
		// rz.setAxis(Rotate.Z_AXIS);
		// rz.setAngle(45.0);
		// rect.getTransforms().add(rz);

		getChildren().addAll(rect, circle, nameLabel, identifierLabel);
	}

	@Override
	protected void resizeGraphic() {

		// int fullSide = side * 2;
		// int transY = height - fullSide;
		// int centerX = width / 2 - side;
		// int centerY = height / 2 - side + transY + 2;
		//
		// int rectSide = (int) (((double) fullSide) / Math.sqrt(2.0));
		//
		// rect.setWidth(rectSide);
		// rect.setHeight(rectSide);
		//
		// // rect.getTransforms().add(Transform.rotate(45, centerX, centerY));
		//
		// // rect.getTransforms().clear();
		//
		// rz.setPivotX(rectSide / 2);
		// rz.setPivotY(rectSide / 2);
		//
		// rect.setTranslateX(centerX + 9);
		// rect.setTranslateY(transY + 8);
		//
		// circle.setTranslateX(centerX);
		// circle.setTranslateY(transY);
		// circle.setCenterX(side);
		// circle.setCenterY(side);
		// circle.setRadius(side);
		//
		// identifierLabel.setTranslateY(transY + 18);
		//
		// nameLabel.resize();
		// identifierLabel.resize();

	}

	@Override
	protected void refreshModelGraphic() {
		CoordinationStep role = (CoordinationStep) semantic;

		// nameLabel.setText(role.getName());
		// identifierLabel.setText(role.getIdentifier());

		nameLabel.refreshModelGraphic();
		identifierLabel.refreshModelGraphic();

	}

	// @Override
	// public Point getClosestPoint(int x, int y) {
	//
	// Point coords = getCompleteCoordinatesThroughParent();
	//
	// double cX = coords.getX() + circle.getTranslateX()
	// + circle.getCenterX();
	// double cY = coords.getY() + circle.getTranslateY()
	// + circle.getCenterY();
	//
	// double vX = x - cX;
	// double vY = y - cY;
	// double magV = Math.sqrt(vX * vX + vY * vY);
	// double aX = cX + vX / magV * circle.getRadius();
	// double aY = cY + vY / magV * circle.getRadius();
	//
	// return new Point((int) aX, (int) aY);
	// }
	//
	// @Override
	// public Point getCenter() {
	//
	// Point coords = getCompleteCoordinatesThroughParent();
	//
	// return new Point(
	// (int) (coords.getX() + circle.getTranslateX() + circle
	// .getCenterX()),
	// (int) (coords.getY() + circle.getTranslateY() + circle
	// .getCenterY()));
	// }

}
